﻿using System.Drawing;
using System.Windows.Forms;
using CloudShot.Editor.CanvasFigures;
using CloudShot.Entities;
using CloudShot.Interfaces;
using CloudShot.Utils;

namespace CloudShot.Editor.Tools
{
  /// <summary>
  ///   Rectangle tool
  /// </summary>
  internal class RectangleTool : ObjectCreationTool<CanvasRectangleBase>
  {
    protected FigureSettings FigureSettings { get; private set; }

    public RectangleTool(IDrawingCanvas drawingCanvas, FigureSettings figureSettings, Section changeToolsSection, Section resetToolSection, Cursor cursor)
      : base(drawingCanvas, changeToolsSection, resetToolSection, cursor)
    {
      FigureSettings = figureSettings;
    }

    public override void OnMouseDown(MouseEventArgs e)
    {
      base.OnMouseDown(e);

      var rectangle = new CanvasRectangle(e.X, e.Y, 1, 1)
      {
        BorderColor = FigureSettings.BorderColor,
        BackColor = FigureSettings.BackColor,
        BorderWidth = FigureSettings.BorderWidth,
        BackgroundOpacity = FigureSettings.BackgroundOpacity,
        LineType = FigureSettings.LineType
      };

      AddNewObject(Canvas, rectangle);
    }

    public override void OnMouseMove(MouseEventArgs e)
    {
      base.OnMouseMove(e);

      if (CreatedObject == null)
        return;

      if (e.Button == MouseButtons.Left)
      {
        var point = new Point(e.X, e.Y);

        using (new AutomaticCanvasRefresher(Canvas, CreatedObject.GetBoundingBox))
        {
          CreatedObject.MoveHandleTo(point, VertexType.RightBottom, Canvas.IsAnyModifierPressed(KeyModifiers.Shift));
        }
      }
    }

    public override void OnMouseUp(MouseEventArgs e)
    {
      base.OnMouseUp(e);

      if (CreatedObject == null)
        return;

      using (new AutomaticCanvasRefresher(Canvas, CreatedObject.GetBoundingBox))
      {
        CreatedObject.Rectangle = CreatedObject.Rectangle.AdjustMinimalRect(FigureSettings.Width, FigureSettings.Height);
      }
    }
  }
}